package com.dinzeer.legendreliclib.lib.util.text;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

// AdvancedGradientTextHelper.java
public class AdvancedGradientTextHelper {
    public enum GradientMode {
        FLOW,      // 流动效果
        WAVE,      // 波浪效果
        PULSE,     // 脉冲效果
        STATIC,     // 静态渐变
        FLOW_REVERSE,//反向流动
        WAVE_REVERSE,//反向波浪
        STATIC_REVERSE,//反向静态渐变
        STATIC_TOP_DOWN,//顶部到底部（未实现）
        STATIC_BOTTOM_UP//底部到顶部（未实现）
        }
    
    // 创建渐变文本
    public static Component createGradientText(String text, ColorPreset preset, GradientMode mode) {
        if (preset == ColorPreset.RAINBOW) {
            return createRainbowText(text, mode);
        }

        return switch (mode) {
            case FLOW -> createFlowGradient(text, preset.getColors());
            case WAVE -> createWaveGradient(text, preset.getColors());
            case PULSE -> createPulseGradient(text, preset.getColors());
            case STATIC -> createStaticGradient(text, preset.getColors());
            case FLOW_REVERSE -> createFlowGradientReverse(text, preset.getColors());
            case WAVE_REVERSE -> createWaveGradientReverse(text, preset.getColors());
            case STATIC_REVERSE -> createStaticGradientReverse(text, preset.getColors());
            case STATIC_TOP_DOWN -> createStaticGradientTopDown(text, preset.getColors());
            case STATIC_BOTTOM_UP -> createStaticGradientBottomUp(text, preset.getColors());
        };
    }

    // 流动渐变效果
    private static Component createFlowGradient(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();
        
        for (int i = 0; i < chars.length; i++) {
            float phase = ((time - i * 0.7f) % chars.length) / (float) chars.length;
            if (phase < 0) phase += 1.0f;
            
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }
    //反向联动
    private static Component createFlowGradientReverse(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();

        for (int i = 0; i < chars.length; i++) {

            float phase = ((time + i * 0.7f) % chars.length) / (float) chars.length;
            if (phase < 0) phase += 1.0f;

            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }

    // 波浪渐变效果
    private static Component createWaveGradient(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();
        
        for (int i = 0; i < chars.length; i++) {
            float phase = ((time * 0.8f - i * 0.4f) % 3.0f) / 3.0f;
            if (phase < 0) phase += 1.0f;
            
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }

   //反向波浪
    private static Component createWaveGradientReverse(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();

        for (int i = 0; i < chars.length; i++) {
            // 反向：使用正号而不是负号，让波浪从右向左传播
            float phase = ((time * 0.8f + i * 0.4f) % 3.0f) / 3.0f;
            if (phase < 0) phase += 1.0f;

            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }
    
    // 脉冲效果（整体同步变化）
    private static Component createPulseGradient(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();
        
        // 所有字符使用相同的相位
        float phase = (float) (0.5f + 0.5f * Math.sin(time * 0.1f));
        
        for (char c : chars) {
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(c))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }
    
    // 静态渐变效果
    private static Component createStaticGradient(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        
        for (int i = 0; i < chars.length; i++) {
            float phase = (float) i / (chars.length - 1);
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }







    // 反向静态渐变效果（从右到左）
    private static Component createStaticGradientReverse(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            // 反向：从右到左
            float phase = (float) (chars.length - 1 - i) / (chars.length - 1);
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }

    // 自上而下渐变效果
    private static Component createStaticGradientTopDown(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();

        // 假设文本高度为1，从上到下渐变
        for (int i = 0; i < chars.length; i++) {
            // 每个字符都使用相同的相位（整体渐变）
            float phase = 0.5f; // 居中位置
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }

    // 自下而上渐变效果
    private static Component createStaticGradientBottomUp(String text, int[] colors) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();

        // 假设文本高度为1，从下到上渐变
        for (int i = 0; i < chars.length; i++) {
            // 每个字符都使用相同的相位（整体渐变）
            float phase = 0.5f; // 居中位置
            int color = interpolateMultiColor(colors, phase);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }







    
    // 彩虹效果
    private static Component createRainbowText(String text, GradientMode mode) {
        MutableComponent result = Component.empty();
        char[] chars = text.toCharArray();
        float time = ClientTimeManager.getAnimationTime();

        for (int i = 0; i < chars.length; i++) {
            float hue;

            switch (mode) {
                case FLOW:
                    hue = ((time * 0.02f - i * 0.1f) % 1.0f);
                    break;
                case FLOW_REVERSE:
                    hue = ((time * 0.02f + i * 0.1f) % 1.0f);
                    break;
                case WAVE:
                    hue = ((time * 0.015f - i * 0.05f) % 1.0f);
                    break;
                case WAVE_REVERSE:
                    hue = ((time * 0.015f + i * 0.05f) % 1.0f);
                    break;
                case PULSE:
                    hue = (time * 0.01f) % 1.0f;
                    break;
                case STATIC:
                    hue = (float) i / chars.length;
                    break;
                case STATIC_REVERSE:
                    hue = (float) (chars.length - 1 - i) / chars.length;
                    break;
                case STATIC_TOP_DOWN:
                case STATIC_BOTTOM_UP:
                    // 垂直方向的彩虹效果使用统一的色调
                    hue = 0.5f;
                    break;
                default:
                    hue = 0.5f;
            }

            if (hue < 0) hue += 1.0f;

            // 使用HSB颜色模式创建彩虹色
            int color = Color.HSBtoRGB(hue, 0.8f, 0.9f);
            result.append(Component.literal(String.valueOf(chars[i]))
                    .withStyle(Style.EMPTY.withColor(color)));
        }
        return result;
    }
    
    // 多颜色插值
    private static int interpolateMultiColor(int[] colors, float phase) {
        if (colors.length == 1) return colors[0];
        
        float segmentLength = 1.0f / (colors.length - 1);
        int segment = (int) (phase / segmentLength);
        
        if (segment >= colors.length - 1) {
            return colors[colors.length - 1];
        }
        
        float segmentPhase = (phase - segment * segmentLength) / segmentLength;
        return interpolateBetweenColors(colors[segment], colors[segment + 1], segmentPhase);
    }
    
    // 两个颜色之间的插值
    private static int interpolateBetweenColors(int color1, int color2, float phase) {
        // 使用缓动函数使过渡更自然
        float easedPhase = easeInOutCubic(phase);
        
        int a1 = (color1 >> 24) & 0xFF, a2 = (color2 >> 24) & 0xFF;
        int r1 = (color1 >> 16) & 0xFF, r2 = (color2 >> 16) & 0xFF;
        int g1 = (color1 >> 8) & 0xFF, g2 = (color2 >> 8) & 0xFF;
        int b1 = color1 & 0xFF, b2 = color2 & 0xFF;
        
        int a = (int) (a1 + (a2 - a1) * easedPhase);
        int r = (int) (r1 + (r2 - r1) * easedPhase);
        int g = (int) (g1 + (g2 - g1) * easedPhase);
        int b = (int) (b1 + (b2 - b1) * easedPhase);
        
        return (a << 24) | (r << 16) | (g << 8) | b;
    }
    
    // 缓动函数
    private static float easeInOutCubic(float x) {
        return x < 0.5 ? 4 * x * x * x : 1 - (float)Math.pow(-2 * x + 2, 3) / 2;
    }
    
    public static List<String> getPresetNames() {
        return Arrays.stream(ColorPreset.values())
                .map(Enum::name)
                .collect(Collectors.toList());
    }
    
    // 工具方法：获取所有渐变模式的名称
    public static List<String> getModeNames() {
        return Arrays.stream(GradientMode.values())
                .map(Enum::name)
                .collect(Collectors.toList());
    }







}